package com.banciyuan.bcywebview.biz.picshow;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.ImageApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.DetailType;

/**
 * Created by squall on 2016/6/7.
 */
public class GetImageOriginHelper {

    private GetOriginInter getOriginInter;
    private Context context;
    private RequestQueue queue;

    public static final String COS = "cos";
    public static final String ILLUST = "illust";
    public static final String DAILY = "daily";
    public static final String GROUP_POST = "group_post";


    public GetImageOriginHelper(Context context) {
        this.context = context;
        this.queue = VolleyQueue.getRquest(context);
    }

    public void getImageOrigin(String type, DetailType detailType, final GetOriginInter getOriginInter) {
        String url = HttpUtils.BASE_URL + ImageApi.getpostCover();
        List<BcyPair> paris = new ArrayList<>();
        paris.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(context).getToken()));
        if (type.equals(COS) || type.equals(ILLUST)) {
            paris.add(new BcyPair(HttpUtils.ID, detailType.getRp_id()));
        } else if (type.equals(DAILY)) {
            paris.add(new BcyPair(HttpUtils.ID, detailType.getUd_id()));
        } else if (type.equals(GROUP_POST)) {
            paris.add(new BcyPair(HttpUtils.ID, detailType.getPost_id()));
        }
        paris.add(new BcyPair(HttpUtils.TYPE, type));
        Map<String, String> map = HttpUtils.getData(paris);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, context)) {
                    getOriginInter.succ(s);
                } else {
                    getOriginInter.fail("");
                }

            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                getOriginInter.fail("");
            }
        };
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        queue.add(stringRequestParam);

    }

    public void getImageOriginSingle(String type, DetailType detailType, String imageurl, final GetOriginInter getOriginInter) {
        String url = HttpUtils.BASE_URL + ImageApi.getDownload();
        List<BcyPair> paris = new ArrayList<>();
        paris.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(context).getToken()));
        if (type.equals(COS) || type.equals(ILLUST)) {
            paris.add(new BcyPair(HttpUtils.ID, detailType.getRp_id()));
        } else if (type.equals(DAILY)) {
            paris.add(new BcyPair(HttpUtils.ID, detailType.getUd_id()));
        } else if (type.equals(GROUP_POST)) {
            paris.add(new BcyPair(HttpUtils.ID, detailType.getPost_id()));
        }
        paris.add(new BcyPair(HttpUtils.URL, imageurl));
        paris.add(new BcyPair(HttpUtils.TYPE, type));
        Map<String, String> map = HttpUtils.getData(paris);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, context)) {
                    getOriginInter.succ(s);
                } else {
                    getOriginInter.fail("");
                }

            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                getOriginInter.fail("");
            }
        };
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        queue.add(stringRequestParam);
    }
}